package cn.mcsys.share.web;

import cn.mcsys.myadmin.common.web.JsonResult;
import cn.mcsys.myadmin.dubbo.service.BackService;
import cn.mcsys.myadmin.modules.air.entity.AirCar;
import cn.mcsys.myadmin.modules.air.entity.AirCarRecord;
import cn.mcsys.myadmin.modules.air.entity.AirRidePoint;
import cn.mcsys.myadmin.modules.platform.entity.AirVehicleGpsBus;
import cn.mcsys.myadmin.modules.platform.entity.AirVehicleGpsTaxi;
import cn.mcsys.myadmin.modules.share.dto.AirCarDTO;
import cn.mcsys.myadmin.modules.share.dto.BookingPeopleDTO;
import cn.mcsys.myadmin.modules.share.entity.AirOutsideCar;
import cn.mcsys.myadmin.modules.viewer.entity.AirTickets;
import cn.mcsys.share.entity.AirCheckRecord;
import cn.mcsys.share.service.AirCheckRecordService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.*;

@Controller
public class ShareController {

    @Autowired
    private BackService backService;

    @Autowired
    private AirCheckRecordService airCheckRecordService;

    @RequestMapping("bookingCount")
    @ResponseBody
    public Map<String, BookingPeopleDTO> dtos() {
        Map<String, BookingPeopleDTO> map = null;
        try {
            List<AirCar> cars = backService.getAllCarList();
            map = new HashMap<>();
//        查询各个乘车点对应的在途数，总预约数，已达到，未送达，
            Map<String,List<AirCar>> pointCars = new HashMap<>();
//        将各个乘车点的车辆进行归类
            for (AirCar car : cars) {
                String pointName = car.getAirRidePoint().getName();
                List<AirCar> airCars = pointCars.get(pointName);
                if(airCars == null){
                    List<AirCar> carList = new ArrayList<>();
                    carList.add(car);
                    pointCars.put(pointName,carList);
                }else{
                    airCars.add(car);
                }
            }
            Set<Map.Entry<String, List<AirCar>>> entries = pointCars.entrySet();
            SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
            for (Map.Entry<String, List<AirCar>> entry : entries) {
                BookingPeopleDTO bookingPeopleDTO = new BookingPeopleDTO();
                map.put(entry.getKey(),bookingPeopleDTO);
                List<AirCar> carsList = entry.getValue();
                AirRidePoint airRidePoint = null;
                for (AirCar airCar : carsList) {
                    airRidePoint = airCar.getAirRidePoint();
                    if (airCar.getStatus() != null){
                        if(airCar.getStatus() == AirCar.RIDING){
//                    查询该车辆最新的发车记录(当天)
                            AirCarRecord airCarRecord = backService.selectLatestCarRecord(airCar,AirCar.RIDING);
                            if(airCarRecord != null){
                                int passengerSum = bookingPeopleDTO.getRidingSum()+ airCarRecord.getPassengerSum();
                                bookingPeopleDTO.setRidingSum(passengerSum);
                            }
                        }
                        if(airCar.getStatus() == AirCar.ARRIVE){
//                    查询该车辆最新的发车记录
                            AirCarRecord airCarRecord = backService.selectLatestCarRecord(airCar,AirCar.ARRIVE);
                            System.out.println("查询该车辆最新的发车记录："+airCarRecord.getId());
                            if(airCarRecord != null){
                                int passengerSum = bookingPeopleDTO.getArriveSum()+ airCarRecord.getPassengerSum();
                                bookingPeopleDTO.setArriveSum(passengerSum);
                            }
                            System.out.println("查询该车辆最新的发车记录："+airCarRecord.getId()+"===============结束");
                        }
                    }
                }
//            当天总预约人数
                int bookingSum = backService.selectBookingSumByPointId(airRidePoint.getId());
                System.out.println("预约总人数："+bookingSum);
                int notArriveSum = bookingSum - bookingPeopleDTO.getArriveSum() - bookingPeopleDTO.getRidingSum();
                System.out.println("未到达人数："+bookingSum);
                bookingPeopleDTO.setNotArriveSum(notArriveSum);
                bookingPeopleDTO.setBookingSum(bookingSum);
                bookingPeopleDTO.setDate(format.format(new Date()));
            }
        }catch (Exception e){
            e.printStackTrace();
        }
        return map;
    }

    /**
     * 获取票务数据
     * @param beginDate
     * @param endDate
     * @return
     * @throws ParseException
     */
    @RequestMapping("getTickets")
    @ResponseBody
    public List<AirTickets> getTickets(String beginDate, String endDate) throws ParseException {
        System.out.println(beginDate);
        System.out.println(endDate);
        SimpleDateFormat format = new SimpleDateFormat("yyyyMMddHHmmss");
        System.out.println(beginDate);
        System.out.println(endDate);
        Date begin = format.parse(beginDate);
        Date end = format.parse(endDate);
        List<AirTickets> tickets = backService.selectTickets(begin,end);
        return tickets;
    }

    @RequestMapping("getCars")
    @ResponseBody
    public List<AirCarDTO> getCars(){
        return backService.trafficList();
    }

/*

    */
/**
     * 获取所有的公交车
     * @return
     *//*

    @RequestMapping("getBusGPS")
    @ResponseBody
    public JsonResult getBusGPS(){
        List<AirVehicleGpsBus> airVehicleGpsBuses = backService.selectAllBus();
        return JsonResult.success(airVehicleGpsBuses);
    }

    */
/**
     * 获取所有的公交车
     * @return
     *//*

    @RequestMapping("getTaxiGPS")
    @ResponseBody
    public JsonResult getTaxiGPS(){
        List<AirVehicleGpsTaxi> airVehicleGpsTaxis = backService.selectAllTaxis();
        return JsonResult.success(airVehicleGpsTaxis);
    }

    */
/**
     * 获取航展专线车辆
     * @return
     *//*

    @RequestMapping("getAirCar")
    @ResponseBody
    public JsonResult getAirCar(){
        List<AirCar> cars = backService.selectAllAirshowCar();
        return JsonResult.success(cars);
    }

    */
/**
     * 获取所有的的跨市专线
     * @return
     *//*

    @RequestMapping("getOutSideCar")
    @ResponseBody
    public JsonResult getOutSideCar(){
        List<AirOutsideCar> airOutsideCars = backService.selectAllOutSideCar();
        return JsonResult.success(airOutsideCars);
    }
*/


    @RequestMapping("checkRecord")
    @ResponseBody
    public List<AirCheckRecord> selectAll(){
        return airCheckRecordService.selectAll();
    }

}
